import Vue from 'vue'
import VueRouter from 'vue-router'
import AdminLayout from '../views/layout/AdminLayout.vue'
import FrontLayout from '../views/layout/FrontLayout.vue'
import tools from "@/utils/utils";

Vue.use(VueRouter)

const routes = [{
		path: '/front',
		name: '',
		component: FrontLayout,
		redirect: "/front/index",
		children: [{
				path: "index",
				name: "index",
				component: () => import('../views/front/Index')
			},
			{
				path: "about",
				name: "about",
				component: () => import('../views/front/About')
			},
			{
				path: "editUserInfo",
				name: "editUserInfo",
				component: () => import('../views/editUserInfo')
			},
			{
				path: "editPassword",
				name: "editPassword",
				component: () => import('../views/editPassword')
			},

			{
				path: "detail",
				name: "detail",
				component: () => import('../views/front/Detail')
			},
			{
				path: "exam",
				name: "exam",
				component: () => import('../views/front/Exam')
			},
			{
				path: "news",
				name: "news",
				component: () => import('../views/front/News')
			},
			{
				path: "score",
				name: "score",
				component: () => import('../views/admin/userScore')
			},
		]
	},
	// {
	// 	path: "/applicationManage",
	// 	name: "applicationManage",
	// 	component: () => import('../views/admin/applicationManage')
	// },
	// {
	// 	path: "examPaper",
	// 	name: "examPaper",
	// 	component: () => import('../views/front/ExamPaper')
	// },
	{
		path: "/examination",
		name: "examination",
		component: () => import('../views/front/examination')
	},
	{
		path: "/instructions",
		name: "instructions",
		component: () => import('../views/front/instructions')
	},
	{
		path: "/login",
		name: "login",
		component: () => import('../views/Login')
	},
	{
		path: "/register",
		name: "register",
		component: () => import('../views/Register')
	},
	{
		path: '*',
		name: 'NotFound',
		meta: {
			name: ''
		},
		component: () => import('../views/404.vue')
	},
]

let router = new VueRouter({
	//路由模式
	// mode: 'history',
	routes
})

export const AddRoutes = () => {
	//从本地存储中进行读取
	//每次调用，都要重新获取菜单
	let routesCopy = {
		path: '/',
		name: 'home',
		component: AdminLayout,
		redirect: "/home",
		children: [{
				path: "/home",
				name: "",
				component: () => import('../views/admin/Home.vue'),
				
			},
			{
				path: '/editUserInfo',
				name: 'editUserInfo',
				component: () => import('../views/editUserInfo.vue'),
				meta: {
					title: "个人信息修改",
				},
			},
			{
				path: '/editPassword',
				name: 'editPassword',
				component: () => import('../views/editPassword.vue'),
				meta: {
					title: "修改密码",
				},
			},
			{
				path: "/questionBank",
				name: "questionBank",
				component: () => import('../views/front/questionBank'),
				meta: {
					title: "题库详情",
				},
			},
			{
				path: "/examPaperBank",
				name: "examPaperBank",
				component: () => import('../views/front/examPaperBank'),
				meta: {
					title: "试卷详情",
				},
			},
			{
				path: "/scorePoint",
				name: "scorePoint",
				component: () => import('../views/front/scorePoint'),
				meta: {
					title: "评分",
				},
			},
		]
	}
	let roleMenu = tools.getRoleMenu()
	console.log(roleMenu)
	roleMenu.forEach(v => {
		if (!v.isPage) {
			routesCopy.children.push({
				path: v.path,
				name: v.path,
				component: (resolve) => require([`@/views/admin/${v.componentPath}`], resolve),
				meta: {
					title: v.title,
				},
			})
		}
		if (v.isPage) {
			v.list.forEach(v => {
				routesCopy.children.push({
					path: v.path,
					name: v.path,
					component: (resolve) => require([`@/views/admin/${v.componentPath}`],
						resolve),
					meta: {
						title: v.title,
					},
				})
			})
		}
	})
	router.addRoute(routesCopy)
}
AddRoutes()
export default router
